<?php
/**
 * types
 * 类别
 * @author Administrator
 * @version
 */
class Model_Tables_Types extends Zend_Db_Table_Abstract {
    /**
     * The default table name
     */
    protected $_name = 'types';
    /**
     * add
     * 增加类别
     * @param $params 字段信息
     */
    public function add( $params )
    {
        $data = array(
		'typename' => $params['typename'],
		'model' => $params['model'],
		'tag1' => $params['tag1'],
		'tag2' => $params['tag2'],
		'tag3' => $params['tag3'],
        'source' => $params['source'],
        'linenum' => $params['linenum'],
        'showtip' => $params['showtip'],
        );
        return $this->insert($data);
    }
    /*
     * 同步类别
     * */
    public function sync( $params )
    {
        $data = array(
        'id' => $params['id'],
		'typename' => $params['typename'],
		'model' => $params['model'],
		'tag1' => $params['tag1'],
		'tag2' => $params['tag2'],
		'tag3' => $params['tag3'],
        'source' => $params['source'],
        //'linenum' => $params['linenum'],
        //'showtip' => $params['showtip'],
        );
        print_r($data);
        if($this->getInfo($params['id'])){
        	return $this->modify($data);
        }else
        {
        	return $this->insert($data);
        }
    }
    /**
     * modify
     * 编辑类别
     * @param $params 字段信息
     * @param $id 主键值
     */
    public function modify( $params, $id )
    {
        $data = array(
		'typename' => $params['typename'],
		'model' => $params['model'],
		'tag1' => $params['tag1'],
		'tag2' => $params['tag2'],
		'tag3' => $params['tag3'],
        'source' => $params['source'],
        'linenum' => $params['linenum'],
        'showtip' => $params['showtip'],
        );
        $where = 'id = '.$id ;
        return $this->update($data , $where );
    }
    /**
     * modifyAjax
     * 无刷新编辑类别
     * @param $id 主键值
     * @param $field 字段
     * @param $value 值
     */
    public function modifyAjax( $id, $field, $value )
    {
    	$db = $this->getAdapter();
    	$data = array($field => $value);
    	$where = $db->quoteInto("id=?", $id);
        return $this->update($data , $where );
    }
    /**
     * modifyAjaxRevers
     * 无刷新字段反转类别
     * @param $field 需要反转的字段
     * @param $id 主键值
     */
    public function modifyAjaxRevers( $field ,$pri)
    {
    	$db = $this->getAdapter();
    	$sql = "update types set  $field =not  $field  where id=' $pri'";
		return $db->query($sql);
    }
    /**
     * del
     * 根据条件删除类别
     * @param $id 主键值
     */
    public function del( $id )
    {
     	$where = 'id = '.$id;
        return $this->delete($where);
    }
    /**
     * getInfo
     * 获取类别信息
     * @param $id 主键值
     * @return 结果数组
     */
    public function getInfo($id)
    {
        $row = $this->fetchRow('id = ' . $id);
        if (!$row) {
        	return null;
        }
        else
        {
        	return $row->toArray();
        }
    }
    /**
     * getCount
     * 获取类别数量
     * @param int  搜索条件
     * @return 结果数
     */
    public function getCount($s = "")
    { 
        $select = $this->select()->from($this->_name,'count(*) as num');
    	if($s!="")  
    	{
    		$db = $this->getAdapter();
			$where = $db->quoteInto("id=?", $s);
    		$select = $select->where($where);
    	}
        $result = $this->fetchRow($select)->num;
        return $result;
    }
    /**
     * getList
     * 取类别数据分页
     * @param int  页码
     * @param int  每页的行数 
     * @param  搜索条件
     * @return 行数组
     */
    public function getList($page, $numPerPage = 10,$s= "")
    {
    	$page = (1>$page) ? 1 : $page;
    	$offset = ($page-1)*$numPerPage;
        $orderby = array('orderid desc');
        if($s!="")     
		{
			$db = $this->getAdapter();
			$where = $db->quoteInto("id= ?", $s);
		}  
        $result = $this->fetchAll($where,$orderby, $numPerPage, $offset )->toArray();
        return $result;
    }
    /*
     * 获取类型列表
     */
 	public function getTypesList($model)
    {
        $db = $this->getAdapter();		
		$sql = "select id,typename from types where model=:model";
		$result = $db->query($sql,array("model"=>$model));
    	$types = array();
        if (!empty($result))
	        {
	            foreach ($result as $item)
	            {
	                $types[$item['id']] = $item['typename'];
	            }
	        }
        return $types;
    }
    /*
     * 获取标签
     */
    public function getTags($typeid){
    	$tags=array();
    	$db = $this->getAdapter();		
		$sql = "select 	tag1, tag2, tag3 from types where id = '$typeid'";
		$result = $db->fetchRow($sql);
		if (!empty($result)){
			$tags["tag1"]=$this->parseTag($result["tag1"]);
			$tags["tag2"]=$this->parseTag($result["tag2"]);
			$tags["tag3"]=$this->parseTag($result["tag3"]);
		}
		return $tags;
    }
    /*
     * 把标签解析成数组
     */
    public function parseTag($tag,$sddAll=false){
    	$result=array();
    	$tagarray=explode(":",$tag);
    	if(count($tagarray)==2){
    		$result["tagname"]=$tagarray[0];
    		$result["tagstr"]=$tagarray[1]; 
    		if($sddAll)
    			$result["tagstr"]="全部|".$tagarray[1];    		
    		$detail=explode("|",$result["tagstr"]);
    		foreach ($detail as $v)
	        {
	        	$result["tagdetail"][$v]=$v;
	        }
    	}
    	else if($sddAll){
	    		$result["tagname"]="";
	    		$result["tagstr"]="";
	    		$result["tagdetail"]["全部"]="全部";
    	}
    	return $result;	
    }
}
